package com.gc.repository.impl;

import com.gc.entity.TeachClass;
import com.gc.repository.TeachClassRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TeachClassRepositoryImpl implements TeachClassRepository {
    @Override
    public boolean Add(TeachClass teachClass) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into teachclass(name, classid, cid, tid) VALUES (?, ?, ?, ?)",
                    teachClass.getName(),
                    teachClass.getClassid(),
                    teachClass.getCid(),
                    teachClass.getTid()
            );

            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<TeachClass> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<TeachClass> query = qr.query("select * from teachclass", new BeanListHandler<>(TeachClass.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from teachclass WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TeachClass FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            TeachClass res = qr.query("select * from teachclass WHERE id = ?", new BeanHandler<>(TeachClass.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public TeachClass FindByClassId(String classId) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            TeachClass res = qr.query("select * from teachclass WHERE classid = ?", new BeanHandler<>(TeachClass.class),
                    classId);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<TeachClass> FindByCourseId(String courseId) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<TeachClass> res = qr.query("select * from teachclass WHERE cid = ?", new BeanListHandler<>(TeachClass.class),
                    courseId);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(TeachClass teachClass) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update teachclass set name = ?, classid = ? , cid = ? , tid = ? WHERE id = ?",
                    teachClass.getName(),
                    teachClass.getClassid(),
                    teachClass.getCid(),
                    teachClass.getTid(),
                    teachClass.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
